package com.wwwc.index.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.ejb.EJBManager;
import com.wwwc.index.ejb.forum.*;

public class ForumServlet extends HttpServlet {
    private Forum ejbDb;
    private ResourceBundle message;
    private String call_path;

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	try {
	    response.setContentType("text/html");
	    PrintWriter out        = response.getWriter();
	    HttpSession session    = request.getSession(true);
	    
	    call_path       = request.getContextPath()+request.getServletPath();
	    String userIp   = request.getRemoteAddr();
	    
	    String lid = (String)session.getAttribute("lid");
	    if(lid == null) {
		lid = "en";
	    }

	    String cid = (String)session.getAttribute("cid");
	    String did = (String)session.getAttribute("did");
	    String mid = (String)session.getAttribute("mid");

	    System.out.println("cid="+cid);
	    System.out.println("did="+did);
	    System.out.println("mid="+mid);

	    message = (ResourceBundle)session.getAttribute("ForumMessage_"+lid);
	    if (message == null) {
		try{
		    message = ResourceBundle.getBundle("Forum_"+lid);
		    session.setAttribute("ForumMessage_"+lid, message);
		}
		catch(Exception e) {
		    System.out.println(e);
		    return;
		}
	    }

	    String table_name  = request.getParameter("table_name");
	    String max_rows    = request.getParameter("max_rows");
	    if(max_rows == null) {
		max_rows = "20";
	    }

	    String action      = request.getParameter("aid");
	    String page_id     = request.getParameter("pid");

	    if(page_id == null) {
		page_id = "1";
	    }

	    UserProfile user_profile = (UserProfile)session.getAttribute("UserProfile");
	    DirectoryDetails dd = (DirectoryDetails)session.getAttribute("LastDirectoryDetails_"+lid);

	    String directory_id = dd.getId();
	    String managers     = dd.getManagers();
	    String admins       = dd.getAdmins();
	    boolean isManager   = ForumUtils.isUserManager(managers, user_profile);
	    boolean isAdmin     = ForumUtils.isUserAdmin(admins, user_profile);

	    if(table_name == null || max_rows == null) {
		System.out.println("ForumServlet:100:missing parameters:table_name, max_rows");
		return;
	    }

	    try {
		if (ejbDb == null) {
		    ejbDb = (Forum)EJBManager.getEJB("java:comp/env/ejb/ForumEJB", "Forum");
		}
	    }
	    catch (Exception e) {
		System.out.println("ForumServlet:Error:101:"+e);
	    }

	    //=========================================================================
	    // Create Forum table
	    //=========================================================================

	    if(ejbDb.tableExists(table_name) == 0) { //Table not found
		if(isAdmin) {
		    String add_table = request.getParameter("add_table");
		    if(add_table == null) {
			out.println("<CENTER><BR><BR>");
			out.println("<FORM action="+call_path+">");
			out.println("Do you want create a new Table ["+table_name+"] ?<BR><BR>");
			out.println("<input type=submit name=add_table value=Yes>");
			out.println("<input type=submit name=add_table value=No>");
			out.println("</FORM>");
			out.println("</CENTER>");
			return;
		    }
		    else {
			if(add_table.equals("Yes")) {
			    out.println(ejbDb.createForumTable(ForumDetails.getCreateTableQuery(table_name)));
			}
			else {
			    out.println("<CENTER><BR><BR><BR>Table is not created.</CENTER>");
			    return;
			}
		    }
		}
		else {
		    out.println("<CENTER>");
		    out.println("<BR><BR><B>Admin message:</B>");
		    out.println("<BR><BR>This section is under maintenance.");
		    out.println("<BR><BR><BR>"+new Date());
		    out.println("</CENTER>");
		    return;
		}
	    }

	    //===========================================================================
	    // Directory Info
	    //===========================================================================
	    String directory_table_name = (String)request.getAttribute("DirectoryTableName");
	    Vector v = (Vector)session.getAttribute("DirectoryListVector_"+directory_table_name);
	    if(v == null) {
		return;
	    }

	    out.println(viewDirectoryInfo (v, dd));

	    //===========================================================================
	    // Page Counter
	    //===========================================================================
	    int total_rows = 0;
	    total_rows = ejbDb.getTableRowCounter(table_name, isAdmin);

	    String search_string =  request.getParameter("sq");
	    int search_total_rows = 0;
	    if(action != null && action.equals("1-1") && search_string != null && (search_string.trim()).length()>0) {
		search_total_rows = ejbDb.getSearchResultRowCounter(table_name, isAdmin, search_string);
		out.println(getSearchPageCounter(search_total_rows, max_rows, page_id, call_path, search_string));
	    }
	    else {
		if(action != null && action.equals("1-1")) {
		    action = null;
		}
		out.println(getPageCounter(total_rows, max_rows, page_id, call_path));
	    }

	    out.println(getAddAndSearchBar(call_path, search_string));

	    out.println("<BR>");
	
	    String fid = request.getParameter("fid");

	    StringBuffer manager_admin_sbf = null;

	    //===========================================================================
	    // manager edit post
	    //===========================================================================
	    if(action != null && action.equals("8-0") && isManager) {
		manager_admin_sbf = new StringBuffer();
		ForumDetails fd = (ForumDetails)ejbDb.getSingleForumDetails(table_name, fid);
		if(fd == null) {
		    out.println("Error!");
		}
		else {
		    boolean updated = false;
		    String update_forum = request.getParameter("update_forum");
		    String error   = null;
		    String title   = fd.getTitle();
		    String poster  = fd.getUser();
		    String date    = ""+fd.getDate();
		    String rating  = ""+fd.getRating();
		    String readers = ""+fd.getReaders();
		    String text    = fd.getText();
		    String links   = fd.getLink();
		    String images  = fd.getImage();
		    String allow_reply = ""+fd.allowReply();
		    String on_top      = ""+fd.onTop();
		    String removed     = ""+fd.isRemoved();

		    if(update_forum != null && update_forum.equals("Save")) {
			title = request.getParameter("title");
			if(title == null || title.length()<1) {
			    error = "Title is required.";
			}

			text    = request.getParameter("text");
			links   = request.getParameter("links");
			images  = request.getParameter("images");
			on_top  = request.getParameter("on_top");

			if(error == null) {
			    error = ejbDb.updateSingleForumDetails(table_name, fid, readers, rating, date,
								   title, poster, text, images, links, allow_reply,
								   on_top, removed);
			}

			if(error == null) {
			    updated = true;
			}
		    }

		    if(!updated) {
			manager_admin_sbf.append("<FORM action="+call_path+">");
			manager_admin_sbf.append("<TABLE width=90% align=center border=1>");

			if(error != null) {
			    manager_admin_sbf.append("<TR>");
			    manager_admin_sbf.append("<TD align=center colspan=2>");
			    manager_admin_sbf.append("<img src=/images/star.gif>"+error+"</TD>");
			    manager_admin_sbf.append("</TR>");
			}

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Title:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=title value='"+title+"' size=100></TD>");
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=\"right\">text:</TD>");
			manager_admin_sbf.append("<TD align=\"left\"><TEXTAREA name=\"text\" rows=\"6\" cols=\"72\" value=\"\">");
			manager_admin_sbf.append(text+"</TEXTAREA></TD>");
			manager_admin_sbf.append("</TR>");
			
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Link:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=links value='"+links+"'  size=100></TD>");
			manager_admin_sbf.append("</TR>");
			
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Image:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=images value='"+images+"'  size=100></TD>");
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>On Top:</TD><TD>");
			if(on_top != null && on_top.equals("true")) {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=on_top value=true checked>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=on_top value=false></TD>");
			}
			else {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=on_top value=true>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=on_top value=false checked></TD>");
			}
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD colspan=2 align=center><input type=submit name=update_forum value=Save></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("</TABLE>");
			manager_admin_sbf.append("<input type=hidden name=fid value="+fid+">");
			manager_admin_sbf.append("<input type=hidden name=aid value="+action+">");
			manager_admin_sbf.append("<input type=hidden name=pid value="+page_id+">");
			manager_admin_sbf.append("</FORM>");
		    }
		}
	    }

	    //===========================================================================
	    // admin edit post
	    //===========================================================================

	    if(action != null && action.equals("9-0") && isAdmin) {
		manager_admin_sbf = new StringBuffer();
		ForumDetails fd = (ForumDetails)ejbDb.getSingleForumDetails(table_name, fid);
		if(fd == null) {
		    out.println("Error!");
		}
		else {
		    boolean updated = false;
		    String update_forum = request.getParameter("update_forum");
		    String error   = null;
		    String title   = fd.getTitle();
		    String poster  = fd.getUser();
		    String date    = ""+fd.getDate();
		    String rating  = ""+fd.getRating();
		    String readers = ""+fd.getReaders();
		    String text    = fd.getText();
		    String links   = fd.getLink();
		    String images  = fd.getImage();
		    String allow_reply = ""+fd.allowReply();
		    String on_top      = ""+fd.onTop();
		    String removed     = ""+fd.isRemoved();

		    if(update_forum != null && update_forum.equals("Save")) {
			title = request.getParameter("title");
			if(title == null || title.length()<1) {
			    error = "Title is required.";
			}

			poster =  request.getParameter("poster");
			if(error == null && (poster == null || poster.length()<1)) {
			    error = "Username is required.";
			}

			date =  request.getParameter("date");
			if(error == null && (date == null || date.length() != 10)) {
			    error = "Date format error.";
			}

			rating  = request.getParameter("rating");
			readers = request.getParameter("readers");
			text    = request.getParameter("text");
			links   = request.getParameter("links");
			images  = request.getParameter("images");
			allow_reply = request.getParameter("allow_reply");
			on_top  = request.getParameter("on_top");
			removed = request.getParameter("removed");

			if(error == null) {
			    error = ejbDb.updateSingleForumDetails(table_name, fid, readers, rating, date,
								   title, poster, text, images, links, allow_reply,
								   on_top, removed);
			}

			if(error == null) {
			    updated = true;
			}
		    }

		    if(!updated) {
			manager_admin_sbf.append("<FORM action="+call_path+">");
			manager_admin_sbf.append("<TABLE width=90% align=center border=1>");

			if(error != null) {
			    manager_admin_sbf.append("<TR>");
			    manager_admin_sbf.append("<TD align=center colspan=2>");
			    manager_admin_sbf.append("<img src=/images/star.gif>"+error+"</TD>");
			    manager_admin_sbf.append("</TR>");
			}

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Title:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=title value='"+title+"' size=100></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Username:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=poster value='"+poster+"'></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Date:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=date value='"+date+"'></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Rating:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=rating value="+rating+"></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Readers:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=readers value="+readers+"></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=\"right\">text:</TD>");
			manager_admin_sbf.append("<TD align=\"left\"><TEXTAREA name=\"text\" rows=\"6\" cols=\"72\" value=\"\">");
			manager_admin_sbf.append(text+"</TEXTAREA></TD>");
			manager_admin_sbf.append("</TR>");
			
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Link:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=links value='"+links+"'  size=100></TD>");
			manager_admin_sbf.append("</TR>");
			
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Image:</TD>");
			manager_admin_sbf.append("<TD><input type=text name=images value='"+images+"'  size=100></TD>");
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Allow Reply:</TD><TD>");
			if(allow_reply != null && allow_reply.equals("true")) {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=allow_reply value=true checked>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=allow_reply value=false></TD>");
			}
			else {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=allow_reply value=true>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=allow_reply value=false checked></TD>");
			}
			manager_admin_sbf.append("</TR>");
			
			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>On Top:</TD><TD>");
			if(on_top != null && on_top.equals("true")) {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=on_top value=true checked>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=on_top value=false></TD>");
			}
			else {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=on_top value=true>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=on_top value=false checked></TD>");
			}
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD align=right>Removed:</TD><TD>");
			if(removed != null && removed.equals("true")) {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=removed value=true checked>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=removed value=false></TD>");
			}
			else {
			    manager_admin_sbf.append("Yes:&nbsp;<input type=radio name=removed value=true>");
			    manager_admin_sbf.append("No:&nbsp;<input type=radio name=removed value=false checked></TD>");
			}
			manager_admin_sbf.append("</TR>");

			manager_admin_sbf.append("<TR>");
			manager_admin_sbf.append("<TD colspan=2 align=center><input type=submit name=update_forum value=Save></TD>");
			manager_admin_sbf.append("</TR>");
			manager_admin_sbf.append("</TABLE>");
			manager_admin_sbf.append("<input type=hidden name=fid value="+fid+">");
			manager_admin_sbf.append("<input type=hidden name=aid value="+action+">");
			manager_admin_sbf.append("<input type=hidden name=pid value="+page_id+">");
			manager_admin_sbf.append("</FORM>");
		    }
		}
	    }

	    //===========================================================================
	    // remove post
	    //===========================================================================
	    if(action != null && action.equals("8-1") && isManager) {
		ejbDb.removeSingleForum(table_name, fid);
	    }

	    //===========================================================================
	    // delete post
	    //===========================================================================
	    if(action != null && action.equals("9-1") && isAdmin) {
		ejbDb.deleteSingleForum(table_name, fid);
	    }

	    //===========================================================================
	    // add new post
	    //===========================================================================
	    if(action != null && action.equals("2-1")) {
		try {
		    request.setAttribute("errorCallBackName","aid");
		    request.setAttribute("errorCallBackValue","2-1");
		    RequestDispatcher rd = request.getRequestDispatcher("/ForumAddNewServlet");
		    rd.include(request,response);
		}
		catch (Exception e) {
		    System.out.println("ForumServlet:Error:2-1"+e);
		}
	    }

	    //============================================================================================
	    // List Search Content 
	    //============================================================================================

	    if(action != null && action.equals("1-1")) {
		ArrayList a = ejbDb.getSearchResultList(table_name, directory_id, 
							(Integer.parseInt(page_id)-1)*Integer.parseInt(max_rows),
							Integer.parseInt(max_rows), isAdmin, search_string);
		
		try {
		    Iterator it = a.iterator();
		    ForumDetails fd = null;
		    int m = 0;
		    out.println("\n<TABLE cellSpacing='0' cellPadding='0' border='0' width='90%' align=center>");	
		    out.println("\n<TR><TD>"+message.getString("TotalFound")+":"+search_total_rows+" ");
		    out.println(message.getString("Records")+".</TD></TR>");	
		    while(it.hasNext()) {
			fd = (ForumDetails)it.next();
			out.println("\n<TR><TD>");
			out.println("<table border=0>");
			out.println("<tr>");
			out.println("<td valign=top><ui><li></li></ui></td>");
			out.println("<FORM name=search_result_"+fd.getFileId()+" action="+call_path+">");
			out.println("<input type=hidden name=aid value=1-1>");
			out.println("<input type=hidden name=pid value="+page_id+">");
			out.println("<input type=hidden name=sq value='"+search_string+"'>");
			out.println("<input type=hidden name=fid value='"+fd.getFileId()+"'>");
			out.println("</FORM>");

			out.println("<td>");
			if(fd.isRemoved()) {
			    out.println("[Removed]&nbsp;");
			}

			out.println("<A href=javaScript:document.search_result_"+fd.getFileId()+".submit();");
			out.println(" style='text-decoration: none;'>");
			out.println(fd.getTitle());
			out.println("</A>");
			out.println("</td>");
	
			out.println("<td valign=top>-<B>"+fd.getUser()+"</B></td>");
			out.println("<td valign=top>("+fd.getSize()+"</td>");
			out.println("<td valign=top>bytes)</td>");
			out.println("<td valign=top>("+fd.getReaders()+"</td>");
			out.println("<td valign=top>Reads)</td>");
			out.println("<td valign=top><I>"+fd.getDate()+"</I></td>");
			out.println("</tr>");
			out.println("</table>");
			out.println("\n</TD></TR>");
			
			if(fid != null && fid.equals(""+fd.getFileId())) {
			    try {
				out.println("<TR><TD>&nbsp;");
				out.println("</TD></TR>");

				out.println("<TR><TD>");
				request.setAttribute("ParentForumDetails", fd);
				RequestDispatcher rd = request.getRequestDispatcher("/ForumDetailsDisplayServlet?isSearch=Yes");
				rd.include(request,response);
				out.println("</TD></TR>");
				out.println("<TR><TD>&nbsp;");
				out.println("</TD></TR>");
			    }
			    catch (Exception e) {
				System.out.println("ForumServlet:listSearchContent:detail:Error:"+e);
			    }
			}
		    }
		    out.println("\n</TABLE>");
		}
		catch (Exception e) {
		    System.out.println("ForumServlet:listSearchContent:Error:"+e);
		}
		return;
	    }

	    //============================================================================================
	    // ListContent (defalut)
	    //============================================================================================

	    ArrayList a = ejbDb.getForumDetailsList(table_name, directory_id, 
						    (Integer.parseInt(page_id)-1)*Integer.parseInt(max_rows),
						    Integer.parseInt(max_rows), isAdmin);
       
	    try {
		Iterator it = a.iterator();
		ForumDetails fd = null;
		int m = 0;
		out.println("\n<TABLE cellSpacing='0' cellPadding='0' border='0' width='90%' align=center>");		
		while(it.hasNext()) {
		    fd = (ForumDetails)it.next();
		    out.println("\n<TR><TD>");
		    out.println("<table border=0>");
		    out.println("<tr>");
		    for(m=0; m<fd.getPosition();m++) {
			out.println("<td>&nbsp;&nbsp;</td>");
		    }
		    out.println("<td valign=top><ui><li></li></ui></td>");
		    out.println("<td>");
		    if(fd.isRemoved()) {
			out.println("[Removed]&nbsp;");
		    }
		    out.println("<A href="+call_path+"?fid="+fd.getFileId()+"&pid="+page_id+" ");
		    out.println(" style='text-decoration: none;'>");
		    out.println(fd.getTitle());
		    out.println("</A></td>");
		    out.println("<td valign=top>-<B>"+fd.getUser()+"</B></td>");
		    out.println("<td valign=top>("+fd.getSize()+"</td>");
		    out.println("<td valign=top>bytes)</td>");
		    out.println("<td valign=top>("+fd.getReaders()+"</td>");
		    out.println("<td valign=top>Reads)</td>");
		    out.println("<td valign=top><I>"+fd.getDate()+"</I></td>");
		    out.println("</tr>");
		    out.println("</table>");
		    out.println("\n</TD></TR>");
		   
		    if(fid != null && fid.equals(""+fd.getFileId())) {
			try {
			    out.println("<TR><TD>&nbsp;");
			    out.println("</TD></TR>");

			    if(manager_admin_sbf != null) {
				out.println("<TR><TD>"+manager_admin_sbf.toString());
				out.println("</TD></TR>");
			    }

			    out.println("<TR><TD>");
			    request.setAttribute("ParentForumDetails", fd);
			    RequestDispatcher rd = request.getRequestDispatcher("/ForumDetailsDisplayServlet");
			    rd.include(request,response);
			    out.println("</TD></TR>");

			    out.println("<TR><TD>&nbsp;");
			    out.println("</TD></TR>");

			    out.println("<TR><TD>");
			    request.setAttribute("ParentForumDetails", fd);
			    request.setAttribute("errorCallBackName","fid");
			    request.setAttribute("errorCallBackValue", ""+fd.getFileId());
			    rd = request.getRequestDispatcher("/ForumAddNewServlet");
			    rd.include(request,response);
			    out.println("</TD></TR>");
			}
			catch (Exception e) {
			    System.out.println("ForumServlet:listContent:detail:Error:"+e);
			}
		    }
		}
		out.println("\n</TABLE>");
	    }
	    catch (Exception e) {
		System.out.println("ForumServlet:listContent:Error:"+e);
	    }

	    //==================================================================
	    // Update readers
	    //==================================================================
	    Hashtable reader_hash = (Hashtable)session.getAttribute("ForumReader");
	    if(reader_hash == null) {
		reader_hash = new Hashtable();
		reader_hash.put(dd.getId(), new ArrayList());
	    }

	    ArrayList rda = (ArrayList)reader_hash.get(dd.getId());

	    if(fid != null && rda != null && rda.indexOf(dd.getId()+":"+fid) == -1) {
		ejbDb.updateForumDetailsReaders(table_name, fid);
		rda.add(dd.getId()+":"+fid);
		reader_hash.put(dd.getId(), rda);
		session.setAttribute("ForumReader", reader_hash);
	    }
	}
	catch(IOException e) {
	    System.out.println("ForumServlet:Error:IOException:"+e);
	}
	catch(Exception e) { 
	    System.out.println("ForumServlet:Error:Exception:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }

    private StringBuffer viewDirectoryInfo (Vector vdd, DirectoryDetails dd)
    {
	String temp = "";
	DirectoryDetails ddt = null;
	Enumeration enum = vdd.elements();
	while(enum.hasMoreElements()) {
	    ddt = (DirectoryDetails)enum.nextElement();
	    if(dd.getId().startsWith(ddt.getId())) {
		temp += ddt.getName()+">";
	    }
	}

	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='2' border='0' align=center");
	sbf.append(" width=100% style='border-bottom:1px solid; font-size: 8pt'>");
	sbf.append("\n<TR><TD>");
	sbf.append("\n<TABLE align=center width=90%><TR>");
	if(temp.length()>0) {
	    sbf.append("\n<TD align=center>"+temp.substring(0, (temp.length()-1))+"</TD>");
	}
	else {
	    sbf.append("\n<TD align=center>"+temp+"</TD>");
	}
	sbf.append("\n<TD align=center>"+message.getString("Manager(s)")+":&nbsp;<I>");

	if(dd.getManagers() != null) {
	    sbf.append(dd.getManagers());
	}

	sbf.append("\n</I></TD>");
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	sbf.append("\n</TD>");
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }

    private StringBuffer getPageCounter(int total_rows, String rows_page, String page_id, String call_path) {
	int page_rows = 25;
	int current_page_id = 0;

	try{
	    if(rows_page != null) {
		page_rows = Integer.parseInt(rows_page);
	    }

	    if(page_id != null) {
		current_page_id = Integer.parseInt(page_id);
	    }
	}
	catch(Exception e) {
	}

	int total_pages = 0;
	if(total_rows%page_rows == 0) {
	    total_pages = total_rows/page_rows;
	}
	else {
	    total_pages = total_rows/page_rows+1;
	}

	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='2' border='0' align=center");
	sbf.append(" width=100% style='border-bottom:1px solid; font-size: 8pt'>");
	sbf.append("<TR><TD align=center>");
	sbf.append("\n<TABLE>");
	sbf.append("\n<TR><TD>"+message.getString("PageNo")+":</TD>\n");	

	int list = 5;

	for(int j=1; j<= total_pages; j++) {
	    if(j == current_page_id) {
		sbf.append("\n<TD><B>["+j+"]</B></TD>\n");
	    }
	    else {
		sbf.append("\n<TD><B>[<A href="+call_path+"?pid="+j+" style='text-decoration: none;'>"+j+"</A>]</B></TD>\n");
	    }

	    if(j>=list) {
		sbf.append("\n<FORM method=post name=page_change action="+call_path+">");
		sbf.append("\n<TD valign=top align=left>");
		sbf.append("\n<SELECT name=pid onChange=this.form.submit(); style='font-size: 8px'>");
		sbf.append("\n<OPTION value=1 selected>></OPTION>");
		for(int m=j+1; m<=total_pages; m++) {
		    if(current_page_id == m) {
			sbf.append("\n<OPTION value="+m+" selected>"+m+"</OPTION>");
		    }
		    else {
			sbf.append("\n<OPTION value="+m+">"+m+"</OPTION>");
		    }
		}
		sbf.append("\n</SELECT></TD>");
		sbf.append("\n</FORM>");
		break;
	    }
	}

	sbf.append("\n<TD>&nbsp;&nbsp;"+message.getString("Total")+"</TD><TD><B>["+(total_pages)+"]</B></TD>");
	sbf.append("<TD>"+message.getString("Page(s)")+"</TD></TR>");
	sbf.append("</TABLE>\n");
	sbf.append("\n</TD>");
	sbf.append("\n</TR>");
	sbf.append("</TABLE>\n");
	return sbf;
    }

    private StringBuffer getSearchPageCounter(int total_rows, String rows_page, String page_id, String call_path, String search_string) {
	int page_rows = 25;
	int current_page_id = 0;

	try{
	    if(rows_page != null) {
		page_rows = Integer.parseInt(rows_page);
	    }

	    if(page_id != null) {
		current_page_id = Integer.parseInt(page_id);
	    }
	}
	catch(Exception e) {
	}

	int total_pages = 0;
	if(total_rows%page_rows == 0) {
	    total_pages = total_rows/page_rows;
	}
	else {
	    total_pages = total_rows/page_rows+1;
	}

	StringBuffer sbf = new StringBuffer();
	sbf.append("\n<TABLE cellSpacing='0' cellPadding='2' border='0' align=center");
	sbf.append(" width=100% style='border-bottom:1px solid; font-size: 8pt'>");
	sbf.append("<TR><TD align=center>");
	sbf.append("\n<TABLE>");
	sbf.append("\n<TR><TD>"+message.getString("TotalFound")+":"+total_rows+" "+message.getString("Records")+".");
	sbf.append(message.getString("PageNo")+":</TD>\n");	

	int list = 5;

	for(int j=1; j<= total_pages; j++) {
	    if(j == current_page_id) {
		sbf.append("\n<TD><B>["+j+"]</B></TD>\n");
	    }
	    else {
		sbf.append("\n<FORM method=post name=page_change_"+j+" action="+call_path+">");
		sbf.append("<input type=hidden name=pid value="+j+">");
		sbf.append("<input type=hidden name=aid value=1-1>");
		sbf.append("<input type=hidden name=sq value='"+search_string+"'>");
		sbf.append("\n</FORM>");
		sbf.append("\n<TD><B>[<A href=javaScript:document.page_change_"+j+".submit(); ");
		sbf.append(" style='text-decoration: none;'>"+j+"</A>]</B></TD>\n");
	    }

	    if(j>=list) {
		sbf.append("\n<FORM method=post name=page_change action="+call_path+">");
		sbf.append("\n<input type=hidden name=aid value=1-1>");
		sbf.append("\n<TD valign=top align=left>");
		sbf.append("\n<SELECT name=pid onChange=this.form.submit(); style='font-size: 8px'>");
		sbf.append("\n<OPTION value=1 selected>></OPTION>");
		for(int m=j+1; m<=total_pages; m++) {
		    if(current_page_id == m) {
			sbf.append("\n<OPTION value="+m+" selected>"+m+"</OPTION>");
		    }
		    else {
			sbf.append("\n<OPTION value="+m+">"+m+"</OPTION>");
		    }
		}
		sbf.append("\n</SELECT></TD>");
		sbf.append("<input type=hidden name=aid value=1-1>");
		sbf.append("<input type=hidden name=sq value='"+search_string+"'>");
		sbf.append("\n</FORM>");
		break;
	    }
	}

	sbf.append("\n<TD>&nbsp;&nbsp;"+message.getString("Total")+"</TD><TD><B>["+(total_pages)+"]</B></TD>");
	sbf.append("<TD>"+message.getString("Page(s)")+"</TD></TR>");
	sbf.append("</TABLE>\n");
	sbf.append("\n</TD>");
	sbf.append("\n</TR>");
	sbf.append("</TABLE>\n");
	return sbf;
    }

    private StringBuffer getAddAndSearchBar(String call_path, String search_string) {
	StringBuffer sbf = new StringBuffer();

	sbf.append("\n<TABLE style='border-bottom:1px solid;' width=100%>");
	sbf.append("\n<TR><TD align=center>");
	sbf.append("\n<TABLE>");
	sbf.append("\n<TR>");
	sbf.append(MyHtml.makeLinkCell(message.getString("AddNewPost"), call_path+"?aid=2-1"));
	sbf.append("\n<TD align='center' bgcolor='#AABBCC' style='BORDER:1px solid #808080'>");
	sbf.append("\n<FORM name='searchForm' method='post' action="+call_path+">");
	if(search_string == null) {
	    sbf.append("\n<input type='text' name='sq' value='' size='50' maxLength='50' style='font-size:6pt; border:0px'>");
	}
	else {
	    sbf.append("\n<input type='text' name='sq' value='"+search_string+"' size='50' maxLength='50' style='font-size:6pt; border:0px'>");
	}
	sbf.append("\n<input type='hidden' name='aid' value='1-1' >");
	sbf.append("\n</TD>");
	sbf.append(MyHtml.makeLinkCell(message.getString("Search"), "javaScript:document.searchForm.submit();"));
	sbf.append("\n</FORM>");
	sbf.append("\n</TR>");
	sbf.append("\n</TABLE>");
	sbf.append("\n</TD></TR>");
	sbf.append("\n</TABLE>");
	return sbf;
    }
}






